package com.neusoft.di;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        System.out.println("Using Stream.dropWhile() and Stream.takeWhile():");
        testDropWhileAndTakeWhile();


        Map<Integer, String> map = new HashMap<>();
        map.put(1, "One");
        map.put(2, "Two");
        map.put(3, null);
        map.put(4, "four");
//        map.entrySet()
//                .stream()
//                .flatMap(e -> e.getValue() == null ? Stream.empty() : Stream.of(e.getValue()))
//                .collect(Collectors.toSet())
//                        .forEach(System.out::println);
        map.entrySet()
                .stream()
                .flatMap(e -> Stream.ofNullable(e.getValue()))
                .collect(Collectors.toSet())
                .forEach(System.out::println);


        System.out.println("Hello world!");
    }

    private static void testDropWhileAndTakeWhile() {
        List<Integer> list = List.of(1, 3, 5, 4, 6, 7, 8, 9);
        System.out.println("Original Stream: " + list);
        List<Integer> list2 = list.stream()
                .dropWhile(n -> n % 2 == 1)
                .toList();
        System.out.println("After using dropWhile(n -> n % 2 == 1): " + list2);

    }
}